﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.BLL;
using CMS.Entity;
using System.Drawing;
using CMS.Common;
using System.IO;
using UserCenter.Common;

public partial class news_ImageManage_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
        }
    }

    private void bd()
    {
        string fid = Request["id"];
        if (!string.IsNullOrEmpty(fid))
        {
            ImageManageBLL imageManageBLL = new ImageManageBLL();
            var image = imageManageBLL.GetOne(fid);
            ViewState["fid"] = fid;
            ImageManageListBLL imageManageListBLL = new ImageManageListBLL();
            var lst = imageManageListBLL.GetLst(fid);
            rep1.DataSource = lst;
            rep1.DataBind();
            txtId.Attributes.Add("readonly", "readonly");
            txtId.Text = image.Id;
            txtTitle.Text = image.Title;
            txtWidth.Text = image.Width.ToString();
            txtHeight.Text = image.Height.ToString();
            if (!string.IsNullOrEmpty(Request["cid"]))
            {
                long cid = long.Parse(Request["cid"]);
                var temp = imageManageListBLL.GetOne(cid);
                txtTitle1.Text = temp.Title;
                txtUrl.Text = temp.Url;

            }
        }
        else
        {
            divUpload.Visible = false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string fid = (string)ViewState["fid"];
        ImageManageBLL imageManageBLL = new ImageManageBLL();
        if (string.IsNullOrEmpty(fid))
        {
            ImageManage c = new ImageManage();
            c.Width = int.Parse(txtWidth.Text);
            c.Height = int.Parse(txtHeight.Text);
            c.Title = txtTitle.Text;
            c.RecTime = DateTime.Now;
            c.Id = txtId.Text;
            imageManageBLL.Add(c);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='edit.aspx?id=" + c.Id + "'", true);
        }
        else
        {
            var c = imageManageBLL.GetOne(fid);
            c.ModTime = DateTime.Now;
            c.Title = txtTitle.Text;
            c.Width = int.Parse(txtWidth.Text);
            c.Height = int.Parse(txtHeight.Text);
            imageManageBLL.Update(c);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('内容保存成功')", true);
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string fid = (string)ViewState["fid"];
        if (FileUpload1.HasFile)
        {
            string fileName = Guid.NewGuid().ToString();
            string url1 = "";
            string url2 = "";
            UpLoadImage(fid, fileName, out url1, out url2);
            ImageManageListBLL imageManageListBLL = new ImageManageListBLL();
            ImageManageList c1 = null;
            if (Request["cid"] == null)
            {
                c1 = new ImageManageList();
            }
            else
            {
                c1 = imageManageListBLL.GetOne(int.Parse(Request["cid"]));
            }
            c1.Fid = fid;
            c1.FileName = fileName;
            c1.ImageUrl = url1;
            c1.Title = txtTitle1.Text;
            c1.Type = "big";
            c1.Url = txtUrl.Text;
            imageManageListBLL.Add(c1);
            ImageManageList c2 = new ImageManageList();
            c2.Fid = fid;
            c2.FileName = fileName;
            c2.ImageUrl = url2;
            c2.Type = "small";
            c2.Title = txtTitle1.Text;
            c2.Url = txtUrl.Text;
            if (Request["cid"] == null)
            {
                c1.RecTime = DateTime.Now;
                c2.RecTime = c1.RecTime;
                imageManageListBLL.Add(c2);
                imageManageListBLL.Add(c1);
            }
            else
            {
                c1.ModTime = DateTime.Now;
                c2.ModTime = c1.ModTime;
                imageManageListBLL.Update(c2);
            }
            CacheClearBLL.ClearLike(fid);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='edit.aspx?id=" + fid + "'", true);
        }
        else if (Request["cid"] == null)
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请选择文件')", true);
        }
        else
        {
            ImageManageListBLL imageManageListBLL = new ImageManageListBLL();
            int cid = int.Parse(Request["cid"]);
            var temp = imageManageListBLL.GetOne(cid);
            temp.Url = txtUrl.Text;
            temp.Title = txtTitle1.Text;
            imageManageListBLL.Update(temp);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('更新成功');document.location='edit.aspx?id=" + fid + "'", true);
            CacheClearBLL.ClearLike(fid);
        }
    }

    void UpLoadImage(string fid, string fileName, out string url1, out string url2)
    {
        Bitmap b = new Bitmap(FileUpload1.PostedFile.InputStream, false);
        //获取数据库中的宽度

        ImageManageBLL imageManageBLL = new ImageManageBLL();
        var image = imageManageBLL.GetOne(fid);
        //判断是以高为标准还是以宽为标准
        int slWidth = 0;
        int slHeight = 0;
        double bl = b.Width * 1.0 / b.Height;
        if (b.Width * 1.0 / b.Height > image.Width * 1.0 / image.Height)
        {
            //以宽为标准
            slWidth = image.Width;
            slHeight = (int)(slWidth * 1.0 / bl);
        }
        else
        {
            slHeight = image.Height;
            slWidth = (int)(slHeight * bl);
        }
        string rootPath = System.Configuration.ConfigurationManager.AppSettings["ImageManageFile"] + DateTime.Now.Year + "/" + DateTime.Now.Month + "/";
        string rootUrl = System.Configuration.ConfigurationManager.AppSettings["ImageManageUrl"] + DateTime.Now.Year + "/" + DateTime.Now.Month + "/";
        string path1 = rootPath + fileName + ".jpg";
        string path2 = rootPath + fileName + "_small.jpg";
        url1 = rootUrl + fileName + ".jpg";
        url2 = rootUrl + fileName + "_small.jpg";
        //原始图片
        var data1 = CommTools.KiResizeImage(b, b.Width, b.Height);
        string msg = "";
        FileDeliver.StartTranCommand(data1, path1, out msg);
        var data2 = CommTools.KiResizeImage(b, slWidth, slHeight);
        FileDeliver.StartTranCommand(data2, path2, out msg);
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string fid = (string)ViewState["fid"];
        if (!string.IsNullOrEmpty(fid))
        {
            Response.Redirect("edit.aspx?id=" + fid);

        }
    }
}